package exam;

import utils.TimeUtils;
import utils.DBUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Shawn
 * Date: 2009-6-25
 * Time: 23:41:02
 */
public class AnswerPaper {

  public static void dbCheck(Connection con, long date) throws SQLException {
    String yyyyMM = TimeUtils.GetyyyyMM(date);

    if (DBUtils.isExist(con, "answerpaper_" + yyyyMM)) return;

    DBUtils.createTable(con, "answerpaper", "answerpaper_" + yyyyMM);
  }

  public static void addAnswerPaperList(Connection con, long date, List<AnswerPaperInfo> answerList) throws SQLException {
    String yyyyMM = TimeUtils.GetyyyyMM(date);
    String sql = "INSERT INTO answerpaper_" + yyyyMM + " VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?)";
    PreparedStatement ps = con.prepareStatement(sql);
    for (AnswerPaperInfo info : answerList) {
      ps.setLong(1, info.getTid());
      ps.setString(2, info.getKm());
      ps.setString(3, info.getDt());
      ps.setString(4, info.getYd());
      ps.setInt(5, info.getTx());
      ps.setString(6, info.getContent());
      ps.setString(7, info.getStandardAnswer());
      ps.setString(8, info.getAnswer());
      ps.executeUpdate();
    }
  }

  public static void delAnswerPaper(Connection con, long date, long tid) throws SQLException {
    String yyyyMM = TimeUtils.GetyyyyMM(date);
    String sql = "DELETE FROM answerpaper_" + yyyyMM + " WHERE tid=" + tid;
    con.createStatement().executeUpdate(sql);
  }

  public static void fillAnswer(Connection con, long date, long aid, String answer) throws SQLException {
    String yyyyMM = TimeUtils.GetyyyyMM(date);
    String sql = "UPDATE answerpaper_" + yyyyMM + " SET answer=? WHERE aid=? LIMIT 1";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setString(1, answer);
    ps.setLong(2, aid);
    ps.executeUpdate();
  }

  public static List<AnswerPaperInfo> getAnswerPaperList(Connection con, long date, long tid) throws SQLException {
    String yyyyMM = TimeUtils.GetyyyyMM(date);
    String sql = "SELECT * FROM answerpaper_" + yyyyMM + " WHERE tid=?";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setLong(1, tid);
    ResultSet rs = ps.executeQuery();

    ArrayList<AnswerPaperInfo> answers = new ArrayList<AnswerPaperInfo>();
    while (rs.next()) answers.add(rs2Info(rs));
    return answers;
  }

  public static AnswerPaperInfo rs2Info(ResultSet rs) throws SQLException {
    AnswerPaperInfo info = new AnswerPaperInfo();
    info.setAid(rs.getLong("aid"));
    info.setTid(rs.getLong("tid"));
    info.setKm(rs.getString("km"));
    info.setDt(rs.getString("dt"));
    info.setYd(rs.getString("yd"));
    info.setTx(rs.getInt("tx"));
    info.setContent(rs.getString("content"));
    info.setStandardAnswer(rs.getString("standardAnswer"));
    info.setAnswer(rs.getString("answer"));
    return info;
  }
}
